package com.example.demo01.Impl.mobileImpl;

import com.example.demo01.Dao.mobileDao.UserInfoDao;
import com.example.demo01.Entity.mobileEntity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class UserInfoDaoImpl implements UserInfoDao {

    @Autowired
    JdbcTemplate jdbcTemplate;

    /**
     * 根据用户名&密码获取用户的基本信息
     * @param account
     * @param password
     * @return
     */
    @Override
    public List<UserInfo> getUserInfoInstanceByAccount(String account, String password) {
        String sql="SELECT \n" +
                "\tuserinfo1.idnum,\n" +
                "\tuserinfo1.name,\n" +
                "\tuserinfo1.nation,\n" +
                "\tuserinfo1.address,\n" +
                "\tuserinfo1.curaddress,\n" +
                "\tTIMESTAMPDIFF(YEAR, userinfo1.birthtime, CURDATE()) as age,\n" +
                "\tuserinfo1.sex,\n" +
                "\tuserinfo1.nurselevel,\n" +
                "\tuserinfo1.occupation,\n" +
                "\tuserinfo1.phonenum\n" +
                "from clientuser,userinfo1\n" +
                "WHERE clientuser.phoneNum=? AND clientuser.password=? AND clientuser.idnum=userinfo1.idnum";
        List<UserInfo> list = jdbcTemplate.query(sql, new Object[]{account, password}, new RowMapper<UserInfo>() {
            @Nullable
            @Override
            public UserInfo mapRow(ResultSet resultSet, int i) throws SQLException {
                return new UserInfo(
                        resultSet.getString("idnum"),
                        resultSet.getString("name"),
                        resultSet.getString("nation"),
                        resultSet.getString("address"),
                        resultSet.getInt("age"),
                        resultSet.getString("sex"),
                        resultSet.getString("curaddress"),
                        resultSet.getString("occupation"),
                        resultSet.getString("nurselevel"),
                        resultSet.getString("phonenum")
                );
            }
        });
        return list;
    }
}
